package com.tourlaid.mamagerment.entity

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.util.StringUtils

/**
 * Created by xiaoyongge on 2017/7/20.
 */
class ResponseData<T> constructor(data: T?) {

    var isSuccess = true

    var msg: String? = null

    var data = data

    var totalCount: Long? = null

    /**
     * 异常

     * @param data
     */
    constructor(msg: String, data: T) : this(data) {
        this.isSuccess = false
        this.msg = msg
    }

    /**
     *
     * @param data
     */
    constructor(totalCount: Long, data: T) : this(data) {
        this.totalCount = totalCount
    }

    constructor() : this(null) {
    }

    fun toJson(callback: String): String {
        val objectMapper = ObjectMapper()
        val str = objectMapper.writeValueAsString(this)
        if(StringUtils.isEmpty(callback)){
            return str
        }else{
            return callback + "(" + str + ")"
        }
    }
}